//
//  HZL_NearbyViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/12/1.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_NearbyViewController.h"
#import "HZL_NearbyCollectionViewCell.h"

@interface HZL_NearbyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIButton *titlebtn;
@property (nonatomic, strong) NSDictionary *titleDic;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, retain) UIView *pointView;
@property (nonatomic, assign) CGFloat w;
@end

@implementation HZL_NearbyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleArr = [NSArray array];
    self.titleDic = [NSDictionary dictionary];

    
    [self createCollectionView];
    
    [self createTitleView];
}
- (void)createTitleView
{
    self.titleDic = @{@"全部":@"0", @"景点":@"11", @"住宿":@"10", @"餐厅":@"5", @"休闲娱乐":@"21", @"购物":@"6"};
    self.titleArr = @[@"全部", @"景点", @"住宿", @"餐厅", @"休闲娱乐", @"购物"];
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, 35)];
    [self.view addSubview:self.titleView];
    
//    NSLog(@"%@", self.titleDic.allKeys);
    self.w = VIEW_WIDTH / 6;
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        self.titlebtn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.titlebtn.frame = CGRectMake(self.w * i, 1, self.w, 30);
        self.titlebtn.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titlebtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.titlebtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.titlebtn setTitle:self.titleArr[i] forState:UIControlStateNormal];
        self.titlebtn.tag = 1000 + i;
        [self.titlebtn addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:self.titlebtn];
    }
    //来回滑动的view
    self.pointView = [[UIView alloc] initWithFrame:CGRectMake(0, 33, self.w, 2)];
    [self.pointView setBackgroundColor:[UIColor grayColor]];
    [self.titleView addSubview:self.pointView];
}
- (void)go:(UIButton *)btn
{
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
        self.pointView.frame = CGRectMake((btn.tag - 1000)*self.w, 33, self.w, 2);
    } completion:^(BOOL finished) {
        
    }];
    [self.collectionView setContentOffset:CGPointMake((btn.tag - 1000)* VIEW_WIDTH, - 64 + 49) animated:NO];
    [self.collectionView reloadData];
}
#pragma mark - 监听collectionView的偏移量  改变pointView的frame
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.collectionView) {
        CGFloat i = scrollView.contentOffset.x / VIEW_WIDTH;
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
            self.pointView.frame = CGRectMake(self.w * i, 33, self.w, 2);
            
        } completion:^(BOOL finished) {
            
        }];
    }
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(VIEW_WIDTH, VIEW_HEIGHT - 64 + 49);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, VIEW_WIDTH);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 64 + 49) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    [self.collectionView registerClass: [HZL_NearbyCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:self.collectionView];
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_NearbyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    NSDictionary *dic = @{@"category":self.titleDic[self.titleArr[indexPath.item]], @"latitude":[NSString stringWithFormat:@"%f", self.latitude], @"longitude":[NSString stringWithFormat:@"%f", self.longitude]};
    cell.dic = dic;
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
